<template>
	<div class="mainor">
		<el-form
			:inline="true"
			:rules="rules"
			:model="monitorForm"
			class="demo-form-inline"
			ref="userListFrom"
			label-width="80px"
		>
			<div
				class="main_spac1 paddingBottom5"
				@click="(mainor_ex = false), (mainor_co = false)"
			>
				<div class="main_white padding10 paddingBottom0">
					<div class="main_crumbs boxdq">
						<div class="main_crumbs_l">
							<a href="javascript:;" @click="$router.back(-1)">
								<i class="el-icon-arrow-down"></i>返回
							</a>
							<em>|</em>
							<span>订单列表</span>
						</div>
					</div>
					<div class="marginBottom5 marginTop15">
						<el-form-item
							label="用户名称"
							prop="form.name_uid"
							class="marginBottom5"
							:rules="rules.name_uid"
						>
							<el-autocomplete
								v-model.trim="monitorForm.form.name_uid"
								ref="company"
								:trigger-on-focus="true"
								@clear="nicknameClear(), $refs.company.$children[0].blur()"
								:fetch-suggestions="
									(queryString, cb) => {
										basicquerySearchAsync(queryString, cb);
									}
								"
								placeholder="用户名称"
								clearable
								class="wid220 main_comm"
								:highlight-first-item="true"
								value-key="nickname"
								@select="
									(item) => {
										basicChange(item);
									}
								"
							>
								<template slot-scope="{ item }">
									<div class="main_name">
										<span>{{ item.nickname }}</span>
										<span class="main_addr" v-if="info.show_member_code == 1">{{
											item.info ? item.info.code : ""
										}}</span>
									</div>
								</template>
							</el-autocomplete>
						</el-form-item>
						<el-form-item
							label="配送日期"
							prop="form.send_time"
							:rules="rules.send_time"
						>
							<el-date-picker
								v-model="monitorForm.form.send_time"
								type="date"
								:editable="false"
								:clearable="false"
								class="time220"
								value-format="yyyy-MM-dd"
								placeholder="选择日期"
							></el-date-picker>
						</el-form-item>
						<el-form-item
							label="配送时间"
							prop="form.send_time_id"
							:rules="rules.send_time_id"
						>
							<el-select
								v-model.trim="monitorForm.form.send_time_id"
								placeholder="配送时间"
								class="wid220"
							>
								<el-option label="无" :value="0"></el-option>
								<el-option
									v-for="(send_time, index) in info.send_time"
									:key="index"
									:label="send_time"
									:value="parseInt(index)"
								></el-option>
							</el-select>
						</el-form-item>
						<el-form-item label="" prop="form.send_time_id" style="">
							<el-checkbox v-model="monitorForm.form.type" class="marginLeft10"
								>临时采购</el-checkbox
							>
							<el-tooltip class="item" effect="dark" placement="top">
								<div slot="content">
									注释：适用临时采购商品后，不入库，直接送到用户的情况
								</div>
								<svg
									aria-hidden="true"
									class="navbar_svg"
									width="16"
									height="16"
								>
									<use xlink:href="#icon-summary_question" />
								</svg>
							</el-tooltip>
						</el-form-item>
						<div
							class="notes"
							v-if="this.credit_status == 1 || this.overdue_status == 1"
						>
							{{
								this.credit_status == 1
									? "当前用户欠款额度已达提醒预设比例！"
									: "当前用户账期已达账期提醒预设！"
							}}
						</div>
						<div class="clearfix"></div>
						<el-collapse-transition>
							<div v-if="monitorForm.form.type">
								<div>
									<el-form-item
										label="采购"
										prop="form.mode"
										style="margin-right: 0"
										:rules="rules.mode"
									>
										<el-select
											v-model.trim="monitorForm.form.mode"
											placeholder="采购方式"
											class="wid110"
										>
											<el-option label="市场自采" value="1"></el-option>
											<el-option label="普通供应" value="2"></el-option>
											<el-option label="直供用户" value="3"></el-option>
										</el-select>
									</el-form-item>
									<el-form-item
										label=""
										prop="form.buyer_id"
										v-if="monitorForm.form.mode == 1"
										:rules="rules.buyer_id"
									>
										<el-select
											v-model.trim="monitorForm.form.buyer_id"
											placeholder="市场自采"
											class="wid220"
										>
											<el-option
												v-for="(buyer, index) in buyer"
												:key="index"
												:label="buyer"
												:value="parseInt(index)"
											></el-option>
										</el-select>
									</el-form-item>
									<el-form-item
										label=""
										prop="form.supplier_id"
										v-if="monitorForm.form.mode == 2"
										:rules="rules.supplier_id"
									>
										<el-select
											v-model.trim="monitorForm.form.supplier_id"
											placeholder="普通供应"
											class="wid220"
										>
											<el-option
												v-for="(supplier, index) in supplier"
												:key="index"
												:label="supplier"
												:value="parseInt(index)"
											></el-option>
										</el-select>
									</el-form-item>
									<el-form-item
										label=""
										prop="form.direct_supplier"
										v-if="monitorForm.form.mode == 3"
										:rules="rules.direct_supplier"
									>
										<el-select
											v-model.trim="monitorForm.form.direct_supplier"
											placeholder="普通供应"
											class="wid220"
										>
											<el-option
												v-for="(supplier, index) in direct_supplier"
												:key="index"
												:label="supplier"
												:value="parseInt(index)"
											></el-option>
										</el-select>
									</el-form-item>
									<el-form-item
										label=""
										prop="form.send_time_id"
										v-if="!monitorForm.form.mode"
									>
										<el-select
											v-model.trim="mode_all"
											placeholder="采购"
											class="wid220"
										>
										</el-select>
									</el-form-item>
									<el-form-item label="" prop="form.up_purchase_price">
										<el-checkbox
											v-model="monitorForm.form.up_purchase_price"
											class="marginLeft10"
											>更新最近一次采购价</el-checkbox
										>
									</el-form-item>
								</div>
							</div>
						</el-collapse-transition>
						<div class="clearfix"></div>
						<el-form-item
							label="备注"
							prop="form.remark"
							class="marginRight0 remark_in"
						>
							<el-input
								v-model.trim="monitorForm.form.remark"
								placeholder="备注"
								clearable
							></el-input>
						</el-form-item>
					</div>
				</div>
			</div>
			<!-- 子账户 -->
			<div
				class="main_spac1"
				style="padding-bottom: 5px; padding-top: 5px"
				@click="(mainor_ex = false), (mainor_co = false)"
			>
				<div class="main_white padding10">
					<div class="mainor_zh boxnr" style="text-align: left">
						<el-form-item
							label="子账户"
							prop="form.sub_zid"
							:rules="rules.sub_zid"
							class="marginBottom0"
							v-if="display_sub_account"
						>
							<el-select
								v-model.trim="monitorForm.form.sub_zid"
								placeholder="子账户"
								class="wid220"
								@change="sub_account"
							>
								<el-option
									v-for="(subAccount, index) in address.subAccount"
									:key="index"
									:label="subAccount.nickname"
									:value="index"
								></el-option>
							</el-select>
						</el-form-item>
						<div style="width: 65%" class="paddingLeft10">
							<span>收货人：{{ address.address.name }}</span>
							<span>{{ address.address.mobile }}</span>
							<span style="width: 80%">{{
								address.address.address + address.address.detail
							}}</span>
						</div>
					</div>
				</div>
			</div>
			<!-- 商品名称 -->
			<div
				class="main_spac1"
				style="padding-top: 5px"
				@click="(mainor_ex = false), (mainor_co = false)"
			>
				<div class="main_white padding10">
					<div class="mainor_sp boxdq" style="">
						<div class="box">
							<ul class="box">
								<li class="box">
									<span> <em>*</em>商品名称： </span>
									<el-autocomplete
										v-model="formsp.value"
										ref="searchSelect"
										class="wid240 main_comm"
										@clear="$refs.searchSelect.$children[0].blur()"
										:fetch-suggestions="
											(queryString, cb) => {
												querySearchAsync(queryString, cb);
											}
										"
										placeholder="商品/助记码/编码"
										clearable
										:highlight-first-item="true"
										value-key="name"
										@select="
											(item) => {
												handleSelect(item);
											}
										"
									>
										<template slot-scope="{ item }">
											<div class="main_name">
												<span>
													{{ item.goods.name }}【{{ item.spu_name.title }}】
												</span>
												<span
													class="main_addr"
													v-if="info.show_sku_code == 1"
													>{{ item.goods_spu_code }}</span
												>
											</div>
										</template>
									</el-autocomplete>
								</li>
								<li class="box">
									<span> <em>*</em>数量： </span>
									<el-input
										v-model.trim="formsp.num"
										ref="focusw2"
										placeholder="数量"
										clearable
										class="wid140"
										@keyup.enter.native="(e) => mainor_sp(e, 1)"
									></el-input>
								</li>
								<li class="box" v-if="monitorForm.form.type">
									<span> <em>*</em>采购价： </span>
									<el-input
										v-model.trim="formsp.purchase_price"
										ref="focusw3"
										placeholder="采购价"
										clearable
										class="wid140"
										@keyup.enter.native="(e) => mainor_sp(e, 2)"
									></el-input>
								</li>
							</ul>
							<div class="marginBottom10">
								<el-button
									type="primary"
									:disabled="mainor_qr_disabled"
									@click="mainor_qr"
									>确认(回车)</el-button
								>
								<el-button class="reset marginLeft5" type="info" @click="reset"
									>重置</el-button
								>
							</div>
						</div>
					</div>
					<div class="main_table main_taf marginTop0" style="min-height: 330px">
						<el-table
							ref="multipleTable"
							:data="monitorForm.list"
							element-loading-text="加载中"
							fit
							border
							highlight-current-row
						>
							<el-table-column label="序号" width="80">
								<template slot-scope="scope">{{ scope.$index + 1 }}</template>
							</el-table-column>

							<el-table-column
								label="商品图片"
								prop="url"
								width="100"
								class-name="main_hei"
							>
								<template slot-scope="scope">
									<div class="demo-image__error mainor_im">
										<el-image fit="fill" :src="scope.row.url">
											<div slot="error" class="image-slot">
												<img :src="puzzleImg" />
											</div>
										</el-image>
									</div>
								</template>
							</el-table-column>
							<el-table-column
								label="商品名称"
								prop="name"
								:show-overflow-tooltip="true"
							/>
							<el-table-column
								label="描述"
								prop="describe"
								:show-overflow-tooltip="true"
							/>
							<el-table-column
								label="单位"
								prop="unit"
								:show-overflow-tooltip="true"
							/>
							<el-table-column
								label="下单数量"
								class-name="main_hei"
								min-width="160"
							>
								<template slot-scope="scope">
									<el-form-item
										:prop="'list.' + scope.$index + '.order_num'"
										:rules="rules.order_num"
										label-width="10px"
										label=" "
									>
										<el-input
											v-model.trim="scope.row.order_num"
											:ref="'order_num' + scope.row.goods_spu_id"
											placeholder="数量"
											class="wid110"
											@input="(val) => inputchnage(val, scope, 2)"
										></el-input>
									</el-form-item>
								</template>
							</el-table-column>
							<el-table-column
								label="下单单价"
								prop="order_price"
								class-name="main_hei"
								min-width="160"
							>
								<template slot-scope="scope">
									<el-form-item
										:prop="'list.' + scope.$index + '.order_price'"
										:rules="rules.order_price"
										label-width="10px"
										label=" "
									>
										<el-input
											v-model.trim="scope.row.order_price"
											:ref="'order_price' + scope.row.goods_spu_id"
											placeholder="单价"
											class="wid110"
										></el-input>
									</el-form-item>
								</template>
							</el-table-column>
							<el-table-column label="下单小计" prop="userNickname">
								<template slot-scope="scope">{{
									$toFixed(scope.row.order_num * scope.row.order_price)
								}}</template>
							</el-table-column>
							<el-table-column
								label="采购数量"
								class-name="main_hei"
								v-if="monitorForm.form.type"
							>
								<template slot-scope="scope">
									{{ scope.row.order_num }}
								</template>
							</el-table-column>
							<el-table-column
								label="采购价"
								prop="purchase_price"
								class-name="main_hei"
								min-width="160"
								v-if="monitorForm.form.type"
							>
								<template slot-scope="scope">
									<el-form-item
										:prop="'list.' + scope.$index + '.purchase_price'"
										:rules="rules.purchase_price"
										label-width="10px"
										label=" "
									>
										<el-input
											v-model.trim="scope.row.purchase_price"
											:ref="'purchase_price' + scope.row.goods_spu_id"
											@input="(val) => inputchnage(val, scope, 0)"
											placeholder="采购价"
											class="wid110"
										></el-input>
									</el-form-item>
								</template>
							</el-table-column>
							<el-table-column
								label="采购小计"
								v-if="monitorForm.form.type"
								prop="purchase_money"
								class-name="main_hei"
								min-width="160"
							>
								<template slot-scope="scope">
									<el-form-item
										:prop="'list.' + scope.$index + '.purchase_money'"
										:rules="rules.purchase_money"
										label-width="10px"
										label=" "
									>
										<el-input
											v-model.trim="scope.row.purchase_money"
											:ref="'purchase_money' + scope.row.goods_spu_id"
											placeholder="采购小计"
											@input="(val) => inputchnage(val, scope, 1)"
											class="wid110"
										></el-input>
									</el-form-item>
								</template>
							</el-table-column>
							<el-table-column
								label="备注"
								prop="remark"
								width="350"
								class-name="main_hei"
							>
								<template slot-scope="scope">
									<el-input
										v-model.trim="scope.row.remark"
										placeholder="备注"
										class="wid240"
									></el-input>
									<el-link type="danger" @click="scdelete(scope.$index)"
										>删除</el-link
									>
								</template>
							</el-table-column>
						</el-table>
					</div>

					<div class="newConBox_p200 paddingBottom10" id="newConBox">
						<div class="newConBox_h32">
							<div
								:class="['mainor_pr boxdq newConBox ', { on: searchBarFixed }]"
							>
								<div class="mainor_prl">
									<el-button
										class="reset marginLeft10"
										type="primary"
										@click="preservation(2), (beforeNext = false)"
										v-repeatClick
										:loading="saveLoading"
										>保存</el-button
									>
									<el-button @click="$router.back(-1)">取消</el-button>
								</div>
								<div class="mainor_prr">
									<!-- 合计品种：{{ monitorForm.list.length }}件
                  <span>合计金额：¥{{ total }}</span> -->
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</el-form>
	</div>
</template>

<script>
	const shuju = {
		list: {
			pid: "",
			category_id: "",
			pic: "",
			url: "",
			name: "",
			describe: "",
			goods_id: "",
			standard_goods_id: "",
			goods_spu_id: "",
			standard_spu_id: "",
			standard_spu_code: "",
			byname: "",
			spu_name: "",
			unit: "",
			order_num: "",
			order_price: "",
			purchase_price: "",
			purchase_money: "",
			purchase_num: "",
			remark: "",
			cycle_id: "",
			show: false,
			discount:''
		},
		form: {
			name_uid: "",
			uid: "",
			nickname: "",
			line_id: "",
			salesman_id: "",
			contact: "",
			mobile: "",
			address: "",
			send_time: "",
			send_time_id: 0,

			remark: "",
			zid: null,
			sub_zid: null,
			type: true, //订单类型 1 临时采购 2正常
			up_purchase_price: true,
			mode: "",
			supplier_id: "",
			buyer_id: "",
			direct_supplier: "",
		},
		formsp: {
			name: [],
			girard: null,
			num: "",
			value: "",
		},
		address: {
			address: {
				name: "",
				mobile: "",
				address: "",
				detail: "",
			},
			info: {},
			subAccount: [],
		},
	};

	import { mapGetters } from "vuex";
	import {
		order_base,
		fast_temporary_info,
		order_getMemberInfo,
		fast_order_add,
		goods_getOfferPrice,
		member_getMembers,
	} from "@/api/order/orderList";
	import { apiUrl, objKeySort, Md5 } from "@/utils/public";
	import rules from "@/rules/order/orderList_add_temporary";
	import { puzzleImg, setting_company, goods_getGoodsSpu } from "@/api/currency";
	import { parseTime } from "@/utils/index";

	export default {
		computed: {
			...mapGetters(["avatar"]),
			total() {
				let sz = 0;
				this.monitorForm.list.forEach((e) => {
					sz += parseFloat(
						this.$toFixed(parseFloat(e.order_num * e.order_price))
					);
				});
				return this.$toFixed(sz);
			},
		},
		watch: {
			"monitorForm.form.mode": {
				handler(val) {
					if (val == 1) {
						this.monitorForm.form.supplier_id = null;
						this.monitorForm.form.direct_supplier = null;
					} else if (val == 2) {
						this.monitorForm.form.buyer_id = null;
						this.monitorForm.form.direct_supplier = null;
					} else if (!val) {
						this.monitorForm.form.supplier_id = null;
						this.monitorForm.form.buyer_id = null;
						this.monitorForm.form.direct_supplier = null;
					} else if (val == 3) {
						this.monitorForm.form.supplier_id = null;
						this.monitorForm.form.buyer_id = null;
					}
				},
				deep: true,
			},
		},
		data() {
			return {
				listLoading: false,
				// 时间
				parseTime,
				// 验证
				rules,
				// 列表数据
				monitorForm: {
					form: {
						name_uid: "",
						uid: "",
						nickname: "",
						line_id: "",
						salesman_id: "",
						contact: "",
						mobile: "",
						address: "",
						send_time: "",
						send_time_id: 0,

						remark: "",
						zid: null,
						sub_zid: null,
						type: true, //订单类型 1 临时采购 2正常
						up_purchase_price: true,
						mode: "",
						supplier_id: "",
						buyer_id: "",
						direct_supplier: "",
					},
					list: [],
				},
				mode_all: "",
				order_price: "",
				goods_remark: "",
				goods_cycle_id: "",
				address: {
					address: {
						name: "",
						mobile: "",
						address: "",
						detail: "",
					},
					info: {},
					subAccount: [],
				},
				puzzleImg,
				info: [],
				credit_status: 0,
				overdue_status: 0,
				// 商品搜索
				formsp: {
					name: [],
					girard: null,
					num: "",
					purchase_price: "",
					value: "",
				},
				show: true,
				display_sub_account: true,
				// 监听滚动
				searchBarFixed: false,
				// 返回
				beforeNext: false,
				lng_lat_form: {
					lng_lat: "",
					lng_lat_mars: "",
				},
				// 确认回车disabled
				mainor_qr_disabled: true,
				// 采购
				supplier: {},
				buyer: {},
				direct_supplier: {},
				auto_calculate_entry_purchase: "",
				saveLoading: false,
			};
		},
		mounted() {
			this.handleScroll();
			window.addEventListener("scroll", this.handleScroll);
			this.myDate();
		},
		destroyed() {
			window.removeEventListener("scroll", this.handleScroll);
		},
		created() {
			this.fast_temporary_info();
			// 信息
			if (this.id) {
				this.order_base()
					.then(() => {
						this.order_getOrderList(this.uid, this.id);
					})
					.catch(() => {});
			} else {
				this.order_base();
			}
		},
		beforeRouteLeave(to, from, next) {
			if (this.beforeNext) {
				setTimeout(() => {
					this.beforeNext = false;
					this.$confirm(
						"您已经有数据操作，离开页面系统不会保存您的操作，确定离开吗？",
						"提示",
						{
							confirmButtonText: "确定",
							cancelButtonText: "取消",
							type: "warning",
						}
					)
						.then(() => {
							// 正常跳转
							next();
						})
						.catch(() => {
							next(false);
						});
				}, 200);
			} else {
				next();
			}
		},
		methods: {
			// 时间
			myDate() {
				var myDate = new Date();
				myDate.setDate(myDate.getDate() + 1);
				this.monitorForm.form.send_time = myDate
					.toLocaleDateString()
					.replace(/\//g, "-");
			},

			// 监听滚动
			handleScroll() {
				let scrollTop =
					window.pageYOffset ||
					document.documentElement.scrollTop ||
					document.body.scrollTop;
				scrollTop + document.documentElement.clientHeight >
				document.getElementById("newConBox").offsetTop + 300
					? (this.searchBarFixed = false)
					: (this.searchBarFixed = true);
			},
			// 用户搜索
			basicquerySearchAsync(queryString, cb) {
				let name = queryString || "";
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(
								Object.assign(apiUrl().name, {
									name: name,
								})
							) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					{
						name: name,
					}
				);
				member_getMembers(data).then((res) => {
					if (res.errcode == 0) {
						var results = res.data.list;
						this.$refs.searchSelect.activated = true;
						cb(results);
					} else {
						this.$message.error(res.errmsg);
						cb([]);
					}
				});
			},
			basicChange(val) {
				this.monitorForm.form.uid = val.id;
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(
								Object.assign(apiUrl().name, {
									id: val.id,
								})
							) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					{
						id: val.id,
					}
				);
				order_getMemberInfo(data).then((res) => {
					if (res.errcode == 0) {
						if (res.data.info.credit_status == 2) {
							this.$alert(
								"当前用户欠款额度已达停止下单比例，无法下单！",
								"提示",
								{
									confirmButtonText: "确定",
									callback: (action) => {
										this.userClear();
									},
								}
							);
						} else {
							if (res.data.info.overdue_status == 2) {
								this.$alert(
									"当前用户账期已超过预设最高账期，无法下单！",
									"提示",
									{
										confirmButtonText: "确定",
										callback: (action) => {
											this.userClear();
										},
									}
								);
							}
						}
						this.credit_status = res.data.info.credit_status;
						this.overdue_status = res.data.info.overdue_status;
						this.monitorForm.list = [];
						let send_time_id = Object.keys(this.info.send_time);

						if (send_time_id.includes(String(res.data.info.send_time_id))) {
							this.monitorForm.form.send_time_id = parseInt(
								res.data.info.send_time_id
							);
						} else {
							this.monitorForm.form.send_time_id = 0;
						}
						this.monitorForm.form.nickname = res.data.subAccount[0].nickname;
						this.monitorForm.form.line_id = res.data.info.line_id;
						this.monitorForm.form.salesman_id = res.data.info.salesman_id;
						this.monitorForm.form.contact = res.data.address.name;
						this.monitorForm.form.mobile = res.data.address.mobile;
						this.monitorForm.form.address =
							res.data.address.address + res.data.address.detail;
						this.monitorForm.form.zid = null;
						this.monitorForm.form.sub_zid = null;
						res.data.subAccount.map((e, ind) => {
							if (ind == 0) {
								e.id = 0;
								e.nickname = "主账号";
							}
						});
						this.address = res.data;
						this.beforeNext = true;
						this.lng_lat_form.lng_lat =
							res.data.address.lng + "," + res.data.address.lat;
						this.lng_lat_form.lng_lat_mars =
							res.data.address.lng_mars + "," + res.data.address.lat_mars;
					} else {
						this.$message.error(res.errmsg);
					}
				});
				// console.log(val)
			},
			nicknameClear() {
				this.userClear();
			},
			// 子账号显示
			setting_company() {
				if (!this.display_sub_account) {
					this.userClear();
				}
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(
								Object.assign(apiUrl().name, {
									display_sub_account: this.display_sub_account ? 1 : 0,
								})
							) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					{
						display_sub_account: this.display_sub_account ? 1 : 0,
					}
				);
				setting_company(data).then((res) => {
					if (res.errcode == 0) {
					} else {
						this.$message.error(res.errmsg);
					}
				});
			},
			sub_account(val) {
				this.monitorForm.form.zid = this.address.subAccount[val].id;
				this.address.address = this.address.subAccount[val].address;
				this.monitorForm.form.contact = this.address.subAccount[val].address.name;
				this.monitorForm.form.mobile =
					this.address.subAccount[val].address.mobile;
				this.monitorForm.form.address =
					this.address.subAccount[val].address.address +
					this.address.subAccount[val].address.detail;
			},

			// 商品搜索
			querySearchAsync(queryString, cb) {
				if (this.monitorForm.form.name_uid == "") {
					this.$message.error("用户名称不能为空");
					this.$refs.company.focus();
					cb([]);
					return;
				}
				if (queryString.trim() == "") {
					cb([]);
					return;
				}
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(
								Object.assign(apiUrl().name, {
									name: queryString.trim(),
								})
							) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					{
						name: queryString.trim(),
						type: this.monitorForm.form.type ? 1 : 2,
					}
				);
				goods_getGoodsSpu(data).then((res) => {
					if (res.errcode == 0) {
						res.data.goods.map((e) => {
							e.name = e.goods.name + "【" + e.spu_unit.title + "】";
						});
						var results = res.data.goods;
						if (this.$refs.searchSelect) {
							this.$refs.searchSelect.activated = true;
						}
						cb(results);
					} else {
						this.$message.error(res.errmsg);
						cb([]);
					}
				});
			},
			// 商品选择
			handleSelect(item) {
				this.formsp.name = item;
				this.formsp.girard = item.id;
				this.mainor_qr_disabled = true;
				this.goods_getOfferPrice();
				setTimeout(() => {
					this.$refs.focusw2.focus();
				}, 100);
			},
			goods_getOfferPrice() {
				let formsp = JSON.parse(JSON.stringify(this.formsp));
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(
								Object.assign(apiUrl().name, {
									uid: this.monitorForm.form.uid,
									group_id: this.address.info.id,
									goods_id: formsp.name.goods_id,
									goods_spu_id: formsp.girard,
								})
							) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					{
						uid: this.monitorForm.form.uid,
						group_id: this.address.info.group_id,
						goods_id: formsp.name.goods_id,
						goods_spu_id: formsp.girard,
					}
				);
				goods_getOfferPrice(data).then((res) => {
					if (res.errcode == 0) {
						this.order_price = res.data.price;
						this.goods_remark = res.data.remark;
						this.goods_cycle_id = res.data.cycle_id;
						this.mainor_qr_disabled = false;
					} else {
						this.$message.error(res.errmsg);
					}
				});
			},
			// 商品选择确认(回车)
			mainor_sp(e, type) {
				switch (type) {
					case 1:
						if (!/^-?\d*\.?\d*$/.test(this.formsp.num)) {
							this.$message.error("请输入数字");
							this.$refs.focusw2.focus();
							return;
						}
						if (this.monitorForm.form.type) {
							this.$refs.focusw3.focus();
						} else {
							this.mainor_qr();
						}

						break;

					case 2:
						if (!/^-?\d*\.?\d*$/.test(this.formsp.purchase_price)) {
							this.$message.error("请输入数字");
							this.$$refs.focusw3.focus();
							return;
						}
						var keyCode = window.event ? e.keyCode : e.which;
						if (keyCode == 13) {
							this.mainor_qr();
						}
						break;
				}
			},
			numChange(val, type) {
				console.log(res);
			},
			// 点击添加
			mainor_qr() {
				let formsp = JSON.parse(JSON.stringify(this.formsp));
				if (!formsp.name.name) {
					this.$message.error("请填写商品名称");
					this.$refs.searchSelect.focus();
					return;
				} else if (formsp.num == "") {
					this.$message.error("请填写数量");
					this.$refs.focusw2.focus();
					return;
				} else if (formsp.purchase_price == "" && this.monitorForm.form.type) {
					this.$message.error("填写采购价");
					this.$refs.focusw2.focus();
					return;
				} else if (!this.mainor_qr_disabled) {
					this.mainor_qr_disabled = true;
					let monitorForm_list = JSON.parse(
						JSON.stringify(this.monitorForm.list)
					);
					let ind = 0;
					let index = 0;
					if (monitorForm_list.length != 0) {
						monitorForm_list.map((e, index) => {
							if (e.goods_spu_id == formsp.girard) {
								ind = e.goods_spu_id;
								return;
							}
						});
						if (ind == 0) {
							this.handle(formsp);
						} else {
							setTimeout(() => {
								let order_num = this.monitorForm.list[index].order_num;
								let purchase_price = this.monitorForm.list[index].purchase_price;
								this.monitorForm.list[index].order_num =
									parseFloat(order_num) + parseFloat(formsp.num);
								this.monitorForm.list[index].purchase_price =
									parseFloat(purchase_price) + parseFloat(formsp.purchase_price);
								this.$refs["order_num" + ind][0].focus();
								this.reset();
								ind = 0;
							}, 100);
						}
					} else {
						this.handle(formsp);
					}
				}
			},
			handle(formsp) {
				console.log(formsp)
				let list = JSON.parse(JSON.stringify(shuju.list));
				formsp.name.goods.images.map((e) => {
					if (e.main == 1) {
						list.url = this.avatar.domain + e.path;
						list.pic = e.path;
					}
				});
				list.pid = formsp.name.goods.pid;
				list.category_id = formsp.name.goods.category_id;
				list.name = formsp.name.goods.name;
				list.goods_id = formsp.name.goods_id;
				list.standard_goods_id = formsp.name.goods.standard_goods_id;
				list.byname =
					formsp.name.goods.use_byname == 1 ? formsp.name.goods.byname : "";
				list.order_price = this.order_price;
				list.goods_spu_id = formsp.name.id;
				list.standard_spu_id = formsp.name.standard_spu_id;
				list.standard_spu_code = formsp.name.standard_spu_code;
				list.spu_name = formsp.name.spu_name.title;
				list.unit = formsp.name.spu_unit.title;
				list.purchase_price = formsp.name.purchase_price;
				list.order_num = formsp.num;
				list.designated_inventory = formsp.name.designated_inventory;
				list.minimum = formsp.name.minimum;
				list.discount = formsp.name.discount;
				if (formsp.name.inventory) {
					list.num = formsp.name.inventory.num;
				} else {
					list.num = 0;
				}
				list.remark = this.goods_remark;
				list.cycle_id = String(this.goods_cycle_id);
				list.describe = formsp.name.describe;
				list.red = false;
				list.purchase_price = formsp.purchase_price;
				if (parseFloat(list.purchase_price) && parseFloat(list.order_num)) {
					list.purchase_money = this.$toFixed(
						parseFloat(list.purchase_price) * parseFloat(list.order_num)
					);
				} else {
					list.purchase_money = 0;
				}

				list.purchase_num = "";
				if (parseFloat(list.purchase_price) > parseFloat(list.order_price)) {
					list.red = true;
				}
				this.monitorForm.list.push(list);
				this.reset();
				setTimeout(() => {
					this.$refs.searchSelect.focus();
				}, 100);
			},
			reset() {
				this.formsp = JSON.parse(JSON.stringify(shuju.formsp));
				this.show = false;
				this.$nextTick(() => {
					this.show = true;
				});
			},
			// 修改采购单价以及小计
			inputchnage(val, scope, type) {
				if (!/^-?\d*\.?\d*$/.test(val)) {
					return;
				}
				if (type == 0 && scope.row.purchase_price && val) {
					scope.row.purchase_money = this.$toFixed(
						parseFloat(scope.row.purchase_price) * parseFloat(scope.row.order_num)
					);
				}
				if (type == 1 && scope.row.order_num && val) {
					// auto_calculate_entry_purchase ==0  自动计算单价   1  不反推单价
					if (this.auto_calculate_entry_purchase == 0) {
						scope.row.purchase_price = this.$toFixed(
							parseFloat(scope.row.purchase_money) /
								parseFloat(scope.row.order_num)
						);
					}
				}

				if (type == 2 && scope.row.purchase_price && val) {
					scope.row.purchase_money = this.$toFixed(
						parseFloat(scope.row.purchase_price) * parseFloat(scope.row.order_num)
					);
				}
			},
			// 删除
			scdelete(index) {
				this.monitorForm.list.splice(index, 1);
			},

			// 保存
			preservation(type) {
				this.$nextTick(() => {
					this.$refs["userListFrom"].validate((valid) => {
						if (valid) {
							if (this.monitorForm.list.length == 0) {
								this.$message.error("请至少添加一个商品");
								return;
							}
							this.fast_order_add(type);
						}
					});
				});
			},
			//  保存到数据库
			fast_order_add(type) {
				this.saveLoading = true;
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name,
					this.order_data().form,
					{
						list: JSON.stringify(this.order_data().list),
					},
					this.lng_lat_form
				);
				fast_order_add(data)
					.then((res) => {
						if (res.errcode == 0) {
							this.saveLoading = false;
							this.$message({
								message: "添加成功",
								type: "success",
							});
							if (type == 1) {
								this.userClear();
							} else {
								this.$router.go(-1);
							}
						} else {
							this.saveLoading = false;
							this.$message.error(res.errmsg);
						}
					})
					.catch(() => {
						this.saveLoading = false;
					});
			},
			order_data() {
				let { sub_zid, name_uid, ...form } = JSON.parse(
					JSON.stringify(this.monitorForm.form)
				);

				form.zid = form.zid ? form.zid : 0;
				form.type = form.type ? 1 : 2;
				form.up_purchase_price = form.up_purchase_price ? 1 : 0;
				if (!this.monitorForm.form.type) {
					form.mode = "";
					form.supplier_id = "";
					form.buyer_id = "";
				}
				form.supplier_id = form.direct_supplier
					? form.direct_supplier
					: form.supplier_id;
				delete form.direct_supplier;
				let sz = JSON.parse(JSON.stringify(this.monitorForm.list));
				let list = sz.map((e) => {
					e.purchase_num = e.order_num;
					if (!this.monitorForm.form.type) {
						e.purchase_num = "";
						e.purchase_money = "";
						e.purchase_price = "";
					}
					let {
						url,
						describe,
						show,
						num,
						designated_inventory,
						minimum,
						red,
						...sz
					} = e;
					return { ...sz };
				});
				return {
					form,
					list,
				};
			},
			userClear() {
				this.credit_status = 0;
				this.overdue_status = 0;
				this.monitorForm.list = [];
				this.address = JSON.parse(JSON.stringify(shuju.address));
				this.monitorForm.form = JSON.parse(JSON.stringify(shuju.form));
				this.myDate();
			},
			// 信息
			order_base() {
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name
				);
				return new Promise((resolve, reject) => {
					order_base(data)
						.then((res) => {
							resolve();
							if (res.errcode == 0) {
								this.type = res.data.field;

								this.display_sub_account =
									res.data.display_sub_account == 1 ? true : false;
								this.info = res.data;
							} else {
								this.$message.error(res.errmsg);
							}
						})
						.catch((error) => {
							reject();
						});
				});
			},
			//采购员
			fast_temporary_info() {
				let data = Object.assign(
					{
						sign: Md5(
							objKeySort(Object.assign(apiUrl().name)) + apiUrl().appsecret
						),
					},
					apiUrl().name
				);
				fast_temporary_info(data).then((res) => {
					if (res.errcode == 0) {
						this.supplier = res.data.supplier;
						this.buyer = res.data.buyer;
						this.direct_supplier = res.data.direct_supplier;
						this.auto_calculate_entry_purchase =
							res.data.auto_calculate_entry_purchase;
					}
				});
			},
		},
	};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.main_name {
	display: flex;
	flex-direction: column;
	justify-content: space-between;
	padding: 5px 0;
	height: 50px;

	.main_addr {
		color: #999;
	}

	span {
		line-height: 20px;
		display: inline;
		height: 20px;
	}
}

.mainor {
	/deep/.el-button--default {
		border-color: $menuActiveText;
		color: $menuActiveText;
	}

	/deep/.el-form {
		margin-top: 10px;

		/deep/.el-form-item--mini.el-form-item,
		/deep/.el-form-item--small.el-form-item {
			margin-bottom: 10px;
		}
	}

	.notes {
		margin-left: 80px;
		color: $colour_red;
		font-size: $font_12;
	}

	.remark_in {
		width: 100%;

		/deep/.el-form-item__content {
			width: 80%;

			.el-input {
				width: 62.5%;
			}
		}
	}

	.mainor_zh {
		padding-top: 5px;
		padding-bottom: 5px;

		em {
			color: $colour_18;
			font-size: $font_14;
			display: inline-block;
			width: 90px;
			text-align: right;
			margin-right: 9px;
		}

		span {
			color: $colour_18;
			font-size: $font_14;
			margin-right: 15px;
		}
	}

	.mainor_sp {
		ul {
			margin-right: 20px;

			li {
				margin-bottom: 10px;

				& > span {
					display: inline-block;
					line-height: 32px;
					color: $colour_18;
					font-size: $font_14;
					text-align: right;

					em {
						color: $colour_red;
						margin-right: 5px;
					}
				}

				&:first-child {
					& > span {
						width: 85px;
					}
				}
			}
		}
	}

	.newConBox.on {
		width: calc(100% - 230px);
		padding-left: 0;
	}

	.mainor_pr {
		margin-left: 10px;
		margin-bottom: 10px;

		/deep/.el-button--default {
			border-color: $subActive;
			color: $menuText;

			&:hover {
				border-color: $menuActiveText;
				color: $menuActiveText;
			}
		}

		.mainor_prr {
			font-size: $font_16;
			color: $colour_18;

			span {
				color: #fe3b3b;
				margin-left: 10px;
			}
		}
	}

	.mainor_ex {
		position: fixed;
		width: 220px;
		height: 100%;
		background: $white;
		box-shadow: 0 6px 10px rgba(0, 21, 41, 0.08);
		top: 0;
		right: 0;
		z-index: 9999;
		transition: all 0.5s;
		transform: translate3d(100%, 0, 0);

		&.on {
			transform: translate3d(0, 0, 0);
		}

		.mainor_exb {
			position: absolute;
			width: 100%;
			height: calc(100% - 60px);
			overflow-y: auto;

			h3 {
				font-size: $font_16;
				color: $colour_18;
				height: 50px;
				line-height: 50px;
				border-bottom: 1px solid $colour_e6;
				padding-left: 20px;
			}

			.mainor_exb_an {
				padding-left: 20px;
				margin-top: 15px;

				/deep/.el-checkbox {
					display: block;
					margin-top: 15px;
					margin-bottom: 15px;
				}
			}
		}

		.mainor_exb_b {
			position: absolute;
			bottom: 0;
			width: 100%;
			border-top: 1px solid $colour_e6;
			height: 55px;
			text-align: center;

			/deep/.el-button--default {
				margin-top: 12px;
				border-color: $subActive;
				color: $menuText;

				&:hover {
					border-color: $menuActiveText;
					color: $menuActiveText;
				}
			}
		}
	}

	.main_table {
		/deep/.el-table--small td {
			padding: 10px 0;
		}
	}

	.main_taf {
		/deep/.el-table--small td {
			padding-top: 0;
		}

		/deep/.el-form-item--small.el-form-item {
			margin-bottom: 16px;
			margin-top: 16px;
			margin-right: 0;
		}
	}

	.mainor_co {
		position: fixed;
		width: 460px;
		height: 100%;
		background: $white;
		box-shadow: 0 6px 10px rgba(0, 21, 41, 0.08);
		top: 0;
		right: 0;
		z-index: 9999;
		transition: all 0.5s;
		transform: translate3d(100%, 0, 0);

		/deep/.el-button--default {
			border: 0;
		}

		&.on {
			transform: translate3d(0, 0, 0);
		}

		.mainor_cob_c {
			position: absolute;
			top: 15px;
			right: 15px;

			i {
				font-size: 22px;
				font-weight: bold;
				color: $colour_6;
			}

			&:hover {
				cursor: pointer;
			}
		}

		.mainor_cob_s {
			margin-top: 50px;
			margin-left: 10px;
			margin-right: 10px;

			p {
				font-size: $font_12;
				color: $colour_red;
				margin-top: 10px;
			}

			/deep/.el-input-group {
				width: 100%;
			}
		}

		.main_table {
			margin-left: 0;
			position: fixed;
			top: 0;
			height: calc(100% - 120px);
			width: 100%;
			margin-top: 120px;

			/deep/.el-table__body-wrapper {
				border-left: 0;

				table {
					border-left: 1px solid #ebeef5;
				}
			}

			/deep/.el-table__fixed-body-wrapper {
				border-right: 0;

				table {
					border-right: 1px solid #ebeef5;
				}
			}

			.main_table_p {
				margin-left: 10px;
				margin-right: 10px;
				height: 100%;
			}

			/deep/.el-table th .cell {
				font-size: $font_14;
			}
		}
	}

	.main_dialog .main_table {
		/deep/.el-table__body-wrapper {
			overflow-y: auto;
			height: 360px;
		}
	}
}
.navbar_svg {
	vertical-align: middle;
	transform: translateY(-1px);
	fill: #606266;
}
</style>
